/*
 * CodeAttribPanel.java
 *
 * Created on January 27, 2002, 4:09 PM
 *
 * Modification Log:
 * 1.00   27th Jan 2002   Tanmay   Original version.
 * 1.01   23rd Mar 2002   Tanmay   Added part edit capability.
 * 1.02   01st May 2002   Tanmay   Added ability to add and delete exception table entries
 * 1.03   11th May 2002   Tanmay   Moved instructions view from list to table.
 * 1.04   11th May 2002   Tanmay   Made tables single selection model.
 *-----------------------------------------------------------------------------------------
 *       10th Sep 2003   Tanmay   Moved to SourceForge (http://classeditor.sourceforge.net)
 *-----------------------------------------------------------------------------------------
 */

package gui.attributes;

import classfile.*;
import classfile.attributes.CodeAttribute;
import classfile.attributes.Attribute;
import guihelper.attributes.CodeExceptionsListTableModel;
import guihelper.attributes.InstructionsTableModel;

import javax.swing.table.TableColumn;
import javax.swing.ListSelectionModel;


/**
 * Copyright (C) 2002-2003  Tanmay K. Mohapatra
 * <br>
 * @author 	Tanmay K. Mohapatra
 * @version     1.04, 11th May, 2002
 */
public class CodeAttribPane extends javax.swing.JPanel implements AttribDisplay {
    private CodeAttribute attribute;
    private ConstantPool constPool;
    private boolean bModifyMode;
    
    /** Creates new form CodeAttribPanel */
    public CodeAttribPane(boolean bModifyMode) {
        this.bModifyMode = bModifyMode;
        initComponents();
        
        txtMaxStack.setEnabled(bModifyMode);
        txtMaxLocals.setEnabled(bModifyMode);
        txtNumAttribs.setEnabled(false);
        btnAdd.setEnabled(bModifyMode);
        btnRemove.setEnabled(bModifyMode);
        btnAddInstr.setEnabled(bModifyMode);
        btnRemoveInstr.setEnabled(bModifyMode);
    }
    
    private void populateCode() {
        txtMaxStack.setText(Integer.toString(attribute.iMaxStack));
        txtMaxLocals.setText(Integer.toString(attribute.iMaxLocals));
        txtNumAttribs.setText(Integer.toString(attribute.codeAttributes.getAttribCount()));
        
        setInstructionsTableModel();
        setExceptionTableModel();
    }

    private void setInstructionsTableModel() {
        InstructionsTableModel thisModel = new InstructionsTableModel(constPool, attribute.code);
        tblInstr.setModel(thisModel);
        thisModel.setEditable(bModifyMode);
        thisModel.setCellEditors(tblExceptions);

        TableColumn thisCol;

        thisCol = tblInstr.getColumnModel().getColumn(0);
        thisCol.setPreferredWidth(60);
        thisCol.setMaxWidth(150);

        thisCol = tblInstr.getColumnModel().getColumn(1);
        thisCol.setPreferredWidth(60);
        thisCol.setMaxWidth(150);

        thisCol = tblInstr.getColumnModel().getColumn(2);
        thisCol.setPreferredWidth(500);
        thisCol.setMaxWidth(800);
    }
    
    private void setExceptionTableModel() {
        CodeExceptionsListTableModel thisModel = new CodeExceptionsListTableModel(constPool, attribute);
        tblExceptions.setModel(thisModel);
        thisModel.setEditable(bModifyMode);
        thisModel.setCellEditors(tblExceptions);
        
        TableColumn thisCol;
        
        thisCol = tblExceptions.getColumnModel().getColumn(0);
        thisCol.setPreferredWidth(30);
        thisCol.setMaxWidth(80);
        
        thisCol = tblExceptions.getColumnModel().getColumn(1);
        thisCol.setPreferredWidth(300);
        thisCol.setMaxWidth(500);
        
        thisCol = tblExceptions.getColumnModel().getColumn(2);
        thisCol.setPreferredWidth(100);
        thisCol.setMaxWidth(150);
        
        thisCol = tblExceptions.getColumnModel().getColumn(3);
        thisCol.setPreferredWidth(100);
        thisCol.setMaxWidth(150);
        
        thisCol = tblExceptions.getColumnModel().getColumn(4);
        thisCol.setPreferredWidth(100);
        thisCol.setMaxWidth(150);
    }
    
    public void setInput(Attribute attribute, ConstantPool constPool) {
        this.attribute = (CodeAttribute)attribute;
        this.constPool = constPool;
        populateCode();
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtMaxStack = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtMaxLocals = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtNumAttribs = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblInstr = new javax.swing.JTable();
        tblInstr.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jPanel5 = new javax.swing.JPanel();
        btnAddInstr = new javax.swing.JButton();
        btnRemoveInstr = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblExceptions = new javax.swing.JTable();
        tblExceptions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jPanel4 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        
        setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints1;
        
        jPanel1.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints2;
        
        jLabel2.setText("Max Stack");
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(jLabel2, gridBagConstraints2);
        
        txtMaxStack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMaxStackActionPerformed(evt);
            }
        });
        
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints2.weightx = 0.1;
        jPanel1.add(txtMaxStack, gridBagConstraints2);
        
        jLabel4.setText("Max Locals");
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.insets = new java.awt.Insets(0, 50, 0, 0);
        jPanel1.add(jLabel4, gridBagConstraints2);
        
        txtMaxLocals.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMaxLocalsActionPerformed(evt);
            }
        });
        
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints2.weightx = 0.1;
        jPanel1.add(txtMaxLocals, gridBagConstraints2);
        
        jLabel6.setText("Attribute Count");
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.insets = new java.awt.Insets(0, 50, 0, 0);
        jPanel1.add(jLabel6, gridBagConstraints2);
        
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints2.weightx = 0.1;
        jPanel1.add(txtNumAttribs, gridBagConstraints2);
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints1.insets = new java.awt.Insets(10, 10, 10, 10);
        gridBagConstraints1.weightx = 1.0;
        add(jPanel1, gridBagConstraints1);
        
        jPanel2.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints3;
        
        jPanel2.setBorder(new javax.swing.border.TitledBorder("Code"));
        tblInstr.setModel(new guihelper.attributes.InstructionsTableModel(null, null));
        jScrollPane1.setViewportView(tblInstr);
        
        gridBagConstraints3 = new java.awt.GridBagConstraints();
        gridBagConstraints3.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints3.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints3.weightx = 1.0;
        gridBagConstraints3.weighty = 1.0;
        jPanel2.add(jScrollPane1, gridBagConstraints3);
        
        btnAddInstr.setText("Add");
        jPanel5.add(btnAddInstr);
        
        btnRemoveInstr.setText("Delete");
        jPanel5.add(btnRemoveInstr);
        
        gridBagConstraints3 = new java.awt.GridBagConstraints();
        jPanel2.add(jPanel5, gridBagConstraints3);
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints1.insets = new java.awt.Insets(0, 10, 10, 0);
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.weighty = 1.0;
        add(jPanel2, gridBagConstraints1);
        
        jPanel3.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints4;
        
        jPanel3.setBorder(new javax.swing.border.TitledBorder("Exceptions"));
        tblExceptions.setModel(new guihelper.attributes.CodeExceptionsListTableModel(null, null));
        jScrollPane3.setViewportView(tblExceptions);
        
        gridBagConstraints4 = new java.awt.GridBagConstraints();
        gridBagConstraints4.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints4.gridheight = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints4.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints4.weightx = 1.0;
        gridBagConstraints4.weighty = 1.0;
        jPanel3.add(jScrollPane3, gridBagConstraints4);
        
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        
        jPanel4.add(btnAdd);
        
        btnRemove.setText("Delete");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });
        
        jPanel4.add(btnRemove);
        
        gridBagConstraints4 = new java.awt.GridBagConstraints();
        jPanel3.add(jPanel4, gridBagConstraints4);
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints1.insets = new java.awt.Insets(0, 0, 10, 10);
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.weighty = 1.0;
        add(jPanel3, gridBagConstraints1);
        
    }//GEN-END:initComponents
    
    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
        int iIndex = tblExceptions.getSelectedRow();
        if(iIndex < 0) return;

        attribute.deleteExceptionTableEntryAt(iIndex);
        setExceptionTableModel();
    }//GEN-LAST:event_btnRemoveActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        attribute.addNewExceptionTableEntry();
        setExceptionTableModel();
    }//GEN-LAST:event_btnAddActionPerformed
    
    private void txtMaxLocalsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMaxLocalsActionPerformed
        String sMaxLocals = txtMaxLocals.getText().trim();
        try {
            attribute.iMaxLocals = Integer.parseInt(sMaxLocals);
        }
        catch(NumberFormatException nfe) {
            sMaxLocals = Integer.toString(attribute.iMaxLocals);
        }
        txtMaxLocals.setText(sMaxLocals);
    }//GEN-LAST:event_txtMaxLocalsActionPerformed
    
    private void txtMaxStackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMaxStackActionPerformed
        String sMaxStack = txtMaxStack.getText().trim();
        try {
            attribute.iMaxStack = Integer.parseInt(sMaxStack);
        }
        catch(NumberFormatException nfe) {
            sMaxStack = Integer.toString(attribute.iMaxStack);
        }
        txtMaxStack.setText(sMaxStack);
    }//GEN-LAST:event_txtMaxStackActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txtMaxStack;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtMaxLocals;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField txtNumAttribs;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblInstr;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JButton btnAddInstr;
    private javax.swing.JButton btnRemoveInstr;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tblExceptions;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnRemove;
    // End of variables declaration//GEN-END:variables
    
}
